/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaclientcp3110.UCM;

import common.UCM.UCController;
import common.DAO.Factory.DAOFactory;
import common.Objects.Product;
import javaclientcp3110.Model.MainModel;
import javaclientcp3110.Model.Product.ProductAddModel;
import javaclientcp3110.View.Product.ProductAddView;

/**
 * UC Add Product
 *
 * Pre-condition: A valid user is logged into the system.
 *
 *  1. User selects to add a product.
 *  2. System displays the add product view.
 *  3. User enters the product details and clicks save
 *  4. System gets product details from view
 *  5. System adds the product to the storage medium
 *  6. System displays a success message.
 *
 * Post-condition: The new product is added to the system
 *
 * Unsuccessful Outcomes:
 *
 *   * 3a) User enters invalid data.
 *        1. System displays an error message
 *        2. System prompts user to try again
 *   * 5a) System is unable to update the storage medium
 *        1. System displays an error message
 * @author Richard Irons
 */
public class UCAddProduct implements UCController {
    private ProductAddModel model;
    private ProductAddView view;

    public UCAddProduct() {
        this(new ProductAddView(),new ProductAddModel());
    }

    public UCAddProduct(ProductAddView view,ProductAddModel model) {
        this.model = model;
        this.view = view;
    }
    public void run() {
// *  1. User selects to add a product.
// *  2. System displays the add product view.
// *  3. User enters the product details and clicks save
        if (validateProduct()) {
// *  4. System gets product details from view
        Product tempProduct = new Product();
        tempProduct.setName(view.getProdName());
        tempProduct.setDescription(view.getProdDescription());
        tempProduct.setPrice(Double.parseDouble(view.getProdPrice()));
        tempProduct.setComponents(view.getProdComponents());
        model.setNewProduct(tempProduct);

        boolean added = model.addNewProduct();
            if (added) {
// *  6. System displays a success message.
                view.successMessage("Product successfully added!");
                MainModel.getInstance().updateData();
                view.clearFields();
            } else {
// *   * 5a) System is unable to update the storage medium
// *        1. System displays an error message
                view.errorMessage("Product could not be added");
            }
        } else {
            
// *   * 3a) User enters invalid data.
// *        1. System displays an error message
// *        2. System prompts user to try again
            view.errorMessage("The entered product details are not valid " +
                "\n Please try again.");
        }

// *  5. System adds the product to the storage medium
    }

    private boolean validateProduct() {
        if (view.getProdName().equals(null)) {
            return false;
        }
        if (view.getProdDescription().equals(null)) {
            return false;
        }
        if (view.getProdComponents() == null || view.getProdComponents().isEmpty()) {
            return false;
        }
        try {
            view.getProdPrice();
        }
        catch (NumberFormatException e) {
            return false;
        }
        return true;
    }
}
